<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="业代昵称" prop="nickName">
          <el-input
            v-model.trim="queryParams.nickName"
            placeholder="请输入业代昵称"
            clearable
            size="small"
            @keyup.enter.native="getDataList"
          />
        </el-form-item>

        <el-form-item label="时间区间:" prop="status">
          <el-date-picker v-model="dateSpan"
                          type="daterange"
                          :unlink-panels="true"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          :default-time="['00:00:00', '23:59:59']"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"/>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column
            type="index"
            header-align="center"
            :index="indexMethod"
            align="center"
            label="编号"
          >
          </el-table-column>
          <el-table-column
            prop="nickName"
            header-align="center"
            align="center"
            label="业代昵称"
          >
          </el-table-column>
          <el-table-column
            prop="loginAccount"
            header-align="center"
            align="center"
            label="登录手机号"
          >
          </el-table-column>
          <el-table-column
            prop="totalUserCount"
            header-align="center"
            align="center"
            label="总关联用户数量"
          >
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                icon="el-icon-search"
                @click="handleDetail(scope.row)"
                >详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="queryParams.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>

      <!-- 弹窗, 新增 / 修改 -->
      <DetailForm
        v-if="visible"
        ref="detailForm"
        @refreshDataList="getDataList"
        :visible.sync="visible"
      ></DetailForm>

    </basic-container>
  </div>
</template>

<script>
import { fetchList } from "@/api/shop_report/custom_user_summary";
import DetailForm from "./detail";

import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        nickName:'',
        startTime:'',
        endTime:''
      },

      dataList: [],
      dateSpan:[],

      totalPage: 0,
      dataListLoading: false,
      visible: false,
    };
  },
  created() {
    this.getDataList();
  },
  components: {
    DetailForm,
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods:{
    /**
     * 获取数据列表
     */
     getDataList() {
      this.dataListLoading = true;
      if(this.dateSpan && this.dateSpan.length > 0){
        this.queryParams.startTime = this.dateSpan[0]
        this.queryParams.endTime = this.dateSpan[1]
      } else {
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
      }
      fetchList(
        Object.assign({
          nickName: this.queryParams.nickName,
          startTime: this.queryParams.startTime,
          endTime: this.queryParams.endTime,
          current: this.queryParams.pageIndex,
          size: this.queryParams.pageSize
        })
      ).then((response) => {
        this.dataListLoading = false;
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
      });
    },

    /**
     * 每页数
     */
     sizeChangeHandle(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageIndex = 1;
      this.getDataList();
    },

    /**
     * 当前页
     */
     currentChangeHandle(val) {
      this.queryParams.pageIndex = val;
      this.getDataList();
    },

    /**
     * 重置
     */
     resetQuery() {
      this.queryParams.pageIndex = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.getDataList();
    },

    /**
     * 获取详情
     */
    handleDetail(row){
      const id = row.id
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.detailForm.init(id, this.queryParams.startTime, this.queryParams.endTime);
      });

    },

    indexMethod(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },

  }
}
</script>